package com.pro.framework.api.enums;

import com.baomidou.mybatisplus.annotation.IEnum;
import com.fasterxml.jackson.annotation.JsonValue;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public interface IEnumInt extends IEnum<Integer> {
    @JsonValue
    Integer getValue();

    @SuppressWarnings("unchecked")
    default IEnumInt fromValue(Integer value) {
        IEnumInt t = fromValueMap(value);
        if (t != null) {
            return t;
        }
        Class<IEnumInt> aClass = (Class<IEnumInt>) this.getClass();
        for (IEnumInt enumConstant : aClass.getEnumConstants()) {
            if (enumConstant.getValue().equals(value)) {
                return enumConstant;
            }
        }
        throw new IllegalArgumentException("Cannot deserialize " + value + " to " + aClass);
    }

    default IEnumInt fromValueMap(Integer value) {
        return null;
    }

    String getLabel();

    default String getColor() {
        return null;
    }

    static <T extends IEnumInt> Map<Integer, T> getMap(T[] values) {
        return Arrays.stream(values).collect(Collectors.toMap(T::getValue, o -> o));
    }
}
